/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* SdProjectTaskCloseService
* 项目任务关闭
*
* 修改纪录
* 2021-10-09 版本：1.0 zhangqianbo 创建。
* @version 版本：1.0
* @author 作者：zhangqianbo
* 创建日期：2021-10-09
*/
package com.topscomm.gpm.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.gpm.pojo.GpmProjectTaskCloseEntity;
import com.topscomm.gpm.pojo.GpmProjectTaskFeedbackEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.FlowDatacodeCache;
import com.topscomm.tap.common.SessionUserBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.gpm.service.IGpmProjectTaskCloseService;
import com.topscomm.gpm.service.impl.auto.GpmProjectTaskCloseServiceAuto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "gpmProjectTaskCloseService")
@Scope(value = "singleton")
public class GpmProjectTaskCloseService extends GpmProjectTaskCloseServiceAuto implements IGpmProjectTaskCloseService {

    FlowDatacodeCache flowDatacodeCache = new FlowDatacodeCache();
    protected List<Map<String, Object>> flowidList = new ArrayList();

    /**
     * @author: zhangqianbo
     * @date:
     * @param entity
     * @throws ServiceException
     * @description:单号生成
     * @modify:
     */
    @Override
    protected void beforeInsert(BasicEntity entity) {
        super.beforeInsert(entity);
        if (entity.getId() <= 0) {// 赋值Id和单号
            long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
            entity.setId(arrayId[0]);
            String docNo = String.valueOf(arrayId[0]);
            docNo = docNo.substring(0, 6) + docNo.substring(9);
            entity.setValue(GpmProjectTaskCloseEntity.FieldDocno, docNo);
        }
    }

    /**
     * @author: zhangqianbo
     * @date:
     * @param entity
     * @throws ServiceException
     * @description:
     * @modify:
     */
    @Override
    protected void beforeSubmit(TapEntity entity, SessionUserBean sessionUserBean) {
        super.beforeSubmit(entity, sessionUserBean);
        GpmProjectTaskCloseEntity gpmProjectTaskCloseEntity = (GpmProjectTaskCloseEntity) entity;
        this.flowidList = flowDatacodeCache.getApprovalFlowListMap("ApprovalFlow_GpmProjectTaskClose",
                gpmProjectTaskCloseEntity.getApplyusercode(), "sortcode");
        if (flowidList.size() > 0) {// 赋值审批流程id
            gpmProjectTaskCloseEntity.setFlowid(ConvertUtil.convertToLong(flowidList.get(0).get("id")));
        } else {
            throw new ServiceException("没有匹配的审批流，请联系管理员！");
        }
        // 判断是否有审批中的单据，有则不让提交
        String taskid = ConvertUtil.convertToString(gpmProjectTaskCloseEntity.getTaskid());
        long id = gpmProjectTaskCloseEntity.getId();
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendLineSingle(" taskid='{0}' and id != {1} and state = 1", taskid,id);
        // 判断项目任务反馈的进度是否达到100%，如果到达，不允许异常关闭
        whereSql.clear();
        whereSql.appendLineSingle(" taskid='{0}'", taskid);
        GpmProjectTaskFeedbackService gpmProjectTaskFeedbackService = (GpmProjectTaskFeedbackService) SpringUtil
                .getBean("gpmProjectTaskFeedbackService");
        List<GpmProjectTaskFeedbackEntity> sdProjectTaskFeedbackEntities = gpmProjectTaskFeedbackService.queryByWhere(whereSql.toString());
        for (GpmProjectTaskFeedbackEntity gpmProjectTaskFeedbackEntity : sdProjectTaskFeedbackEntities) {
            if ("100".equals(gpmProjectTaskFeedbackEntity.getTaskschedule())) {
                throw new ServiceException("任务进度已达到100%，无需申请异常关闭！");
            }
        }
    }

}
